package org.example.utils.post.reply;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.dao.post.PCommentDao;
import org.example.dao.dao.post.PLikeDao;
import org.example.dao.dao.post.PPostDao;
import org.example.dao.dao.post.PReplyDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.post.PostRedisDto;
import org.example.dao.entity.post.PComment;
import org.example.dao.entity.post.PLike;
import org.example.dao.entity.post.PReply;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.BeanUtils;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.redis.RedisConstants;
import org.example.skytool.redis.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.example.skytool.redis.RedisConstants.*;
import static org.example.skytool.redis.RedisConstants.POST_REPLY_CACHE_KIND;

@Component
public class RedisReplyService {



    @Resource
    private ContextUtil contextUtil;

    @Resource
    private PReplyDao pReplyDao;


    @Resource
    private IdWorker idWorker;


    @Resource
    private PLikeDao pLikeDao;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PCommentDao pCommentDao;

    @Resource
    private UUserDao uUserDao;

    @Resource
    private PPostDao pPostDao;

    public String getReplyCacheHeader(String id) {
        String header = POST_MOUDLE + POST_REPLY_CONTROLLER + POST_REPLY_CACHE_KIND + id +
                POST_REPLY_CACHE_DATA_KIND;
        return header;
    }

    public String getReplyLikeMemberHeader(String id) {
        String header = POST_MOUDLE + POST_REPLY_CONTROLLER + POST_REPLY_CACHE_KIND + id +
                POST_REPLY_CACHE_MEMBER_KIND;
        return header;
    }

    public Boolean judgeIsMember(String id1, String id2) {
        if (redisUtils.sIsMember(getReplyLikeMemberHeader(id1), id2)) {
            return true;
        } else {
            return false;
        }
    }

    public void getReplyCache(PReply reply) {
        Long id = reply.getId();

        Map map = redisUtils.hashGetAll(getReplyCacheHeader(id.toString()));
        PostRedisDto postRedisDto = BeanUtils.mapToBean(PostRedisDto.class, map);
        if (postRedisDto != null && postRedisDto.getLikeNum() != null) {
            reply.setLikeNum(Integer.valueOf(postRedisDto.getLikeNum()));
        }

        pReplyDao.updateById(reply);
    }

    public void setReplyCache(PReply reply) {
        PostRedisDto postRedisDto = new PostRedisDto(reply);
        String id = reply.getId().toString();
        redisUtils.hashPut(getReplyCacheHeader(id), BeanUtils.beanToMap(postRedisDto));
        setReplyLikeMember(reply);
    }

    public void setReplyLikeMember(PReply reply){
        String id = reply.getId().toString();
        LambdaQueryWrapper<PLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PLike::getLinkId, id);
        lambdaQueryWrapper.eq(PLike::getIsValid, 1);
        List<PLike> list = pLikeDao.selectList(lambdaQueryWrapper);
        String key = getReplyLikeMemberHeader(id);
        List<String> list1 = new ArrayList<>();
        for (PLike pLike : list) {
            list1.add(pLike.getUserId().toString());
        }
        if (list.size() == 0) {

        } else {
            redisUtils.setAdd(key, list1);
        }

    }

    public Long likeReply(String replyId) {
        String userId = contextUtil.getId().toString();
        PReply pReply = pReplyDao.selectById(replyId);
        if (pReply == null) {
            throw new ParamLossErrorException("id传递错误，传递的是:" + replyId);
        }
        Long likeNum;
        if (redisUtils.sIsMember(getReplyLikeMemberHeader(replyId), userId)) {
            redisUtils.sRemove(getReplyLikeMemberHeader(replyId), userId);
            likeNum = redisUtils.hashIncrBy(getReplyCacheHeader(replyId), POST_REPLY_CACHE_MEMBER_LIKE_KIND, -1);
        } else {
            redisUtils.sAdd(getReplyLikeMemberHeader(replyId), userId);
            likeNum = redisUtils.hashIncrBy(getReplyCacheHeader(replyId), POST_REPLY_CACHE_MEMBER_LIKE_KIND, 1);

            PLike pLike = new PLike();
            pLike.setId(idWorker.nextId());
            pLike.setUserId(Long.valueOf(userId));
            pLike.setLinkId(Long.valueOf(replyId));
            pLikeDao.insert(pLike);
        }
        return likeNum;
    }

}
